*****************************
*** GENDER GAP ANALYSIS *****
*** QUINN M. ALBAUGH ********
*** NOVEMBER 2023 VERSION ***
*****************************

*** SET WORKING DIRECTORY
clear all

*** CHANGE SETTINGS FOR GRAPHS
set more off, permanently
grstyle init
grstyle set plain, horizontal nogrid
graph set window fontface "Helvetica"

*** IMPORT DATA
use "${here}GenderGapHighQuality.dta", clear

*************************************
*** BIVARIATE RELATIONSHIP GRAPHS ***
*************************************

drop if genderid == 4

recode partyid (7=6)
recode partyid (8=7)
label define parties3 1 "Liberal" 2 "Conservative" 3 "NDP" 4 "BQ" 5 "Green" 6 "Other" 7 "None"
label values partyid parties3

tab partyid, gen(pid_)

recode pt (12=11) (13=11)

**********************
*** WEIGHTED, 2021 ***
**********************

**************************************
*** FIGURE 1: PARTY IDENTIFICATION ***
**************************************

*** LIBERAL
preserve
drop if genderid == .
drop if pid_1 == .

tab genderid pid_1 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN = sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = pid_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(30.58, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_PID_LIB_W, replace
restore

*** CONSERVATIVE
preserve
drop if genderid == .
drop if pid_2 == .

tab genderid pid_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = pid_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(26.75, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_PID_CON_W, replace
restore

*** NDP
preserve
drop if genderid == .
drop if pid_3 == .

tab genderid pid_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(17.42, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_PID_NDP_W, replace
graph export "NDPPID_W.png", height(1200) width(1600) replace
restore

*** COMBINE PARTY IDENTIFICATION GRAPHS
graph combine HQ_PID_LIB_W.gph HQ_PID_CON_W.gph HQ_PID_NDP_W.gph , col(1) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Identifying with Party", size(small))
graph export "${here}HQ_GenderPID_Weighted.png", height(2400) width(2000) replace

******************************************
*** FIGURE 2: WEIGHTED VOTE INTENTIONS ***
******************************************

*** LIBERAL
preserve
drop if genderid == .
drop if vote_1 == .

tab genderid vote_1 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = vote_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(28.69, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_Vote_LIB_W, replace
restore

*** CONSERVATIVE
preserve
drop if genderid == .
drop if vote_2 == .

tab genderid vote_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = vote_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(31.17, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_Vote_CON_W, replace
restore

*** NDP
preserve
drop if genderid == .
drop if vote_3 == .

tab genderid vote_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = vote_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100, labsize(medsmall)) ///
	yline(24.42, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_Vote_NDP_W, replace
restore

*** COMBINE VOTE INTENTION GRAPHS
graph combine HQ_Vote_LIB_W.gph HQ_Vote_CON_W.gph HQ_Vote_NDP_W.gph, col(1) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Intending to Vote for Party", size(small))
graph export "HQ_GenderVoteIntention_Weighted.png", height(2400) width(2000) replace


******************************************************************
*** FIGURE 3: WEIGHTED PARTY IDENTIFICATION, LGBTQ ONLY SAMPLE ***
******************************************************************

tab partyid if lgbtq == 1 [aweight = cps21_weight_general_all]
tab genderid partyid if lgbtq == 1 [aweight = cps21_weight_general_all], row

*** LIBERAL
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_1 == .

tab genderid pid_1 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = pid_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(29.79, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_PID_LIB_W_LGBTQ, replace
restore

*** CONSERVATIVE
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_2 == .

tab genderid pid_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = pid_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(12.84, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_PID_CON_W_LGBTQ, replace
restore

*** NDP
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_3 == .

tab genderid pid_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(30.67, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_PID_NDP_W_LGBTQ, replace
restore

*** COMBINE PARTY IDENTIFICATION GRAPHS
graph combine HQ_PID_LIB_W_LGBTQ.gph HQ_PID_CON_W_LGBTQ.gph HQ_PID_NDP_W_LGBTQ.gph , col(1) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Identifying with Party", size(small))
graph export "${here}HQ_GenderPID_Weighted_LGBTQ.png", height(2400) width(2000) replace


************************************************************
*** FIGURE 4: WEIGHTED VOTE INTENTION, LGBTQ ONLY SAMPLE ***
************************************************************

tab voteintention if lgbtq == 1 [aweight = cps21_weight_general_all]
tab genderid voteintention if lgbtq == 1 [aweight = cps21_weight_general_all], row

*** LIBERAL
preserve
keep if lgbtq == 1
drop if genderid == .
drop if vote_1 == .

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = vote_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(26.87, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_Vote_LIB_W_LGBTQ, replace
restore

*** CONSERVATIVE
preserve
keep if lgbtq == 1
drop if genderid == .
drop if vote_2 == .

tab genderid vote_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = vote_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(16.49, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_Vote_CON_W_LGBTQ, replace
restore

*** NDP
preserve
keep if lgbtq == 1
drop if genderid == .
drop if vote_3 == .

tab genderid vote_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = vote_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100, labsize(medsmall)) ///
	yline(39.87, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save HQ_Vote_NDP_W_LGBTQ, replace
restore

*** COMBINE VOTE INTENTION GRAPHS
graph combine HQ_Vote_LIB_W_LGBTQ.gph HQ_Vote_CON_W_LGBTQ.gph HQ_Vote_NDP_W_LGBTQ.gph, col(1) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Intending to Vote for Party", size(small))
graph export "${here}HQ_GenderVoteIntention_Weighted_LGBTQ.png", height(2400) width(2000) replace

**********************************************************
*** GENERATE SURVEY ATTRITION RATES BY GENDER IDENTITY ***
**********************************************************
/*
gen pes = 0
replace pes = 1 if pes21_weight_general_all ~= .
tab genderid pes, row chi
*/

***************************
*** MULTIPLE IMPUTATION ***
***************************

*** POST-ELECTION WAVE
use "GenderGapHighQuality.dta", clear
drop if noncitizen == 1
drop if genderid == 4
recode pt (13=11) (12=11)
recode voteintention (5=6)
recode partyid (5=7) (6=7)
gen votechoice = votechoice2021
recode votechoice (5=6)

set matsize 11000
ice m.sexualid4 m.education o.income born_outside_canada indigenous racialized m.mother_tongue4 ///
	m.partyid m.voteintention m.votechoice ///
	immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy international_trade government_intervention income_inequality wealth_gap ///
	i.genderid i.age4 i.pt i.user_language, ///
	saving(HQ_MICE.dta, replace) m(10) seed(123) ///
	cmd(immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion international_trade government_intervention income_inequality wealth_gap: regress)


****************************************************
*** EXAMINE ISSUE ATTITUDES AND CONSTRUCT SCALES ***
****************************************************

use "${here}HQ_MICE.dta", clear

keep genderid sexualid4 education income born_outside_canada indigenous racialized mother_tongue4 ///
	partyid voteintention votechoice ///
	immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy international_trade government_intervention income_inequality wealth_gap ///
	age4 pt user_language cps21_weight_general_all ///
	_mi _mj

mi import ice, automatic clear

*** EXAMINE RELATIONSHIPS AMONG ISSUE ATTITUDES
corr immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy international_trade government_intervention income_inequality wealth_gap if _mi_m ~= 0
alpha immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy government_intervention income_inequality wealth_gap if _mi_m ~= 0
factor immigration refugees jobs_vs_environment_pes family_values bilingualism equal_rights immigrant_integration immigrant_jobs abortion conversion_therapy government_intervention income_inequality wealth_gap if _mi_m ~= 0

*** GENERATE LEFT-RIGHT SCALE
gen left = (immigration + refugees + jobs_vs_environment_pes + family_values + bilingualism + equal_rights + immigrant_integration + immigrant_jobs + abortion + conversion_therapy + government_intervention + income_inequality + wealth_gap) / 13

*** GENERATE IMMIGRATION SCALE
gen immigration_scale = (immigration + refugees + immigrant_integration + immigrant_jobs) / 4
alpha immigration refugees immigrant_integration immigrant_jobs
factor immigration refugees immigrant_integration immigrant_jobs

*** GENERATE REDISTRIBUTION/INEQUALITY SCALE
gen redistribution_scale = (income_inequality + wealth_gap) / 2
alpha income_inequality wealth_gap

*** GENERATE TRADITIONALISM SCALE
gen traditionalism_scale = (family_values + equal_rights) / 2
alpha family_values equal_rights

*** GENERATE VECTOR OF DUMMY VARIABLES FOR PARTY IDENTIFICATION
tab partyid, gen(pid_)

*** GENERATE VECTOR OF DUMMY VARIABLES FOR VOTE CHOICE
tab votechoice, gen(vote_)

*** RECODE INCOME
recode income (1=2)
gen income7 = income - 1
label variable income7 "Income"

**********************************************************
*** FIGURE 5: ISSUE ATTITUDES AND PARTY IDENTIFICATION ***
**********************************************************

*** LIBERAL PARTY IDENTIFICATION
** MODEL 1: NO CONTROLS
mi estimate: logit pid_1 i.genderid [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model1IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (1) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 2: PRIOR DEMOGRAPHICS
mi estimate: logit pid_1 i.genderid i.age4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model2IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (2) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 3: ALL DEMOGRAPHICS
mi estimate: logit pid_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model3IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (3) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 4: ALL DEMOGRAPHICS + CONVERSION THERAPY
mi estimate: logit pid_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt i.conversion_therapy [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model4IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (4) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 5: ALL DEMOGRAPHICS + LEFT-RIGHT SCALE
mi estimate: logit pid_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model5IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (5) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

*** GRAPH MODELS FOR LIBERAL PARTY IDENTIFICATION
preserve
use "HQ_Model1IssuesLiberalPartyID", clear
append using "HQ_Model2IssuesLiberalPartyID"
append using "HQ_Model3IssuesLiberalPartyID"
append using "HQ_Model4IssuesLiberalPartyID"
append using "HQ_Model5IssuesLiberalPartyID"

*** CHECK RELATIVE SIZE OF GAPS
gen BiggerGap2 = 0
replace BiggerGap2 = 1 if abs(Gap2) > abs(Gap1)
tab BiggerGap2

gen BiggerGap3 = 0
replace BiggerGap3 = 1 if abs(Gap3) > abs(Gap1)
tab BiggerGap3

*** DROP THESE VARIABLES FOR RESHAPING
drop BiggerGap2 BiggerGap3

*** RESHAPE FROM MODEL-LEVEL TO MODEL-COMPARISON-LEVEL
reshape long Gap Lower Upper p, i(Model) j(Comparison)

*** LABEL COMPARISONS
label define comparisons 1 "Woman-Man" 2 "Nonbinary-Man" 3 "Nonbinary-Woman"
label values Comparison comparisons

*** CONVERT ESTIMATES FROM PROPORTIONS TO PERCENTAGES
replace Gap = Gap * 100
gen Lower95 = Lower * 100
gen Upper95 = Upper * 100
gen Lower90 = Gap - ((Gap - Lower95) * 0.837)
gen Upper90 = Gap + ((Upper95 - Gap) * 0.837)

*** CODE SIGNIFICANCE AT P < 0.1
gen Sig90 = 0
replace Sig90 = 1 if p < 0.1

gen Sig95 = 0
replace Sig95 = 1 if p < 0.05

*** LOOK AT SIGNIFICANCE BY COMPARISON
tab Comparison Sig90
tab Comparison Sig95
*** ALL GAPS IN LIBERAL PARTY IDENTIFICATION ARE SIGNIFICANT

*** REPLACE NUMBERS AND GENERATE VALUE LABELS
gen ModelLabelled = (Model - 6) * -1

label define models ///
			1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls"
label values ModelLabelled models
			
*** GRAPH COMPARISONS
graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 2, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 2, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 2, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Man") ///
	xlabel(-30(10)0) ///
	xscale(range(-35 5)) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Liberal" "Party Identification", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesLiberalPID_NonbinaryVSMan.gph, replace	

graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 3, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 3, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 3, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Woman") ///
	xscale(range(-35 5)) ///
	xlabel(-30(10)0) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Liberal" "Party Identification", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesLiberalPID_NonbinaryVSWoman.gph, replace	
restore

*** CONSERVATIVE PARTY IDENTIFICATION
** MODEL 1: NO CONTROLS
mi estimate: logit pid_2 i.genderid [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model1IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (1) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 2: PRIOR DEMOGRAPHICS
mi estimate: logit pid_2 i.genderid i.age4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model2IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (2) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 3: ALL DEMOGRAPHICS
mi estimate: logit pid_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model3IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (3) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 4: ALL DEMOGRAPHICS + CONVERSION THERAPY
mi estimate: logit pid_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt i.conversion_therapy [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model4IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (4) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 5: ALL DEMOGRAPHICS + LEFT-RIGHT SCALE
mi estimate: logit pid_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model5IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (5) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

*** GRAPH MODELS FOR CONSERVATIVE PARTY IDENTIFICATION
preserve
use "HQ_Model1IssuesConservativePartyID", clear
append using "HQ_Model2IssuesConservativePartyID"
append using "HQ_Model3IssuesConservativePartyID"
append using "HQ_Model4IssuesConservativePartyID"
append using "HQ_Model5IssuesConservativePartyID"

*** CHECK RELATIVE SIZE OF GAPS
gen BiggerGap2 = 0
replace BiggerGap2 = 1 if abs(Gap2) > abs(Gap1)
tab BiggerGap2

gen BiggerGap3 = 0
replace BiggerGap3 = 1 if abs(Gap3) > abs(Gap1)
tab BiggerGap3

*** DROP THESE VARIABLES FOR RESHAPING
drop BiggerGap2 BiggerGap3

*** RESHAPE FROM MODEL-LEVEL TO MODEL-COMPARISON-LEVEL
reshape long Gap Lower Upper p, i(Model) j(Comparison)

*** LABEL COMPARISONS
label define comparisons 1 "Woman-Man" 2 "Nonbinary-Man" 3 "Nonbinary-Woman"
label values Comparison comparisons

*** CONVERT ESTIMATES FROM PROPORTIONS TO PERCENTAGES
replace Gap = Gap * 100
gen Lower95 = Lower * 100
gen Upper95 = Upper * 100
gen Lower90 = Gap - ((Gap - Lower95) * 0.837)
gen Upper90 = Gap + ((Upper95 - Gap) * 0.837)

*** CODE SIGNIFICANCE AT P < 0.1
gen Sig90 = 0
replace Sig90 = 1 if p < 0.1

gen Sig95 = 0
replace Sig95 = 1 if p < 0.05

*** LOOK AT SIGNIFICANCE BY COMPARISON
tab Comparison Sig90
tab Comparison Sig95
*** ALL GAPS IN CONSERVATIVE PARTY IDENTIFICATION ARE SIGNIFICANT

*** REPLACE NUMBERS AND GENERATE VALUE LABELS
gen ModelLabelled = (Model - 6) * -1

label define models ///
			1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls"
label values ModelLabelled models
			
*** GRAPH COMPARISONS
graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 2, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 2, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 2, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Man") ///
	xlabel(-30(10)0) ///
	xscale(range(-35 5)) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Conservative" "Party Identification", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesConservativePID_NonbinaryVSMan.gph, replace	

graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 3, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 3, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 3, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Woman") ///
	xscale(range(-35 5)) ///
	xlabel(-30(10)0) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Conservative" "Party Identification", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesConservativePID_NonbinaryVSWoman.gph, replace	
restore

*** NDP PARTY IDENTIFICATION
** MODEL 1: NO CONTROLS
mi estimate: logit pid_3 i.genderid [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model1IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (1) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 2: PRIOR DEMOGRAPHICS
mi estimate: logit pid_3 i.genderid i.age4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model2IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (2) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 3: ALL DEMOGRAPHICS
mi estimate: logit pid_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model3IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (3) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 4: ALL DEMOGRAPHICS + CONVERSION THERAPY
mi estimate: logit pid_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt i.conversion_therapy [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model4IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (4) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 5: ALL DEMOGRAPHICS + LEFT-RIGHT SCALE
mi estimate: logit pid_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model5IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (5) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

*** GRAPH MODELS FOR NDP PARTY IDENTIFICATION
preserve
use "HQ_Model1IssuesNDPPartyID", clear
append using "HQ_Model2IssuesNDPPartyID"
append using "HQ_Model3IssuesNDPPartyID"
append using "HQ_Model4IssuesNDPPartyID"
append using "HQ_Model5IssuesNDPPartyID"

*** CHECK RELATIVE SIZE OF GAPS
gen BiggerGap2 = 0
replace BiggerGap2 = 1 if abs(Gap2) > abs(Gap1)
tab BiggerGap2

gen BiggerGap3 = 0
replace BiggerGap3 = 1 if abs(Gap3) > abs(Gap1)
tab BiggerGap3

*** DROP THESE VARIABLES FOR RESHAPING
drop BiggerGap2 BiggerGap3

*** RESHAPE FROM MODEL-LEVEL TO MODEL-COMPARISON-LEVEL
reshape long Gap Lower Upper p, i(Model) j(Comparison)

*** LABEL COMPARISONS
label define comparisons 1 "Woman-Man" 2 "Nonbinary-Man" 3 "Nonbinary-Woman"
label values Comparison comparisons

*** CONVERT ESTIMATES FROM PROPORTIONS TO PERCENTAGES
replace Gap = Gap * 100
gen Lower95 = Lower * 100
gen Upper95 = Upper * 100
gen Lower90 = Gap - ((Gap - Lower95) * 0.837)
gen Upper90 = Gap + ((Upper95 - Gap) * 0.837)

*** CODE SIGNIFICANCE AT P < 0.1
gen Sig90 = 0
replace Sig90 = 1 if p < 0.1

gen Sig95 = 0
replace Sig95 = 1 if p < 0.05

*** LOOK AT SIGNIFICANCE BY COMPARISON
tab Comparison Sig90
tab Comparison Sig95
*** ALL GAPS IN NDP PARTY IDENTIFICATION ARE SIGNIFICANT

*** REPLACE NUMBERS AND GENERATE VALUE LABELS
gen ModelLabelled = (Model - 6) * -1

label define models ///
			1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls"
label values ModelLabelled models
			
*** GRAPH COMPARISONS
graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 2, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 2, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 2, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Man") ///
	xscale(range(-10 65)) ///
	xlabel(-10(10)60) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("NDP" "Party Identification", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesNDPPID_NonbinaryVSMan.gph, replace	

graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 3, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 3, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 3, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Woman") ///
	xscale(range(-10 65)) ///
	xlabel(-10(10)60) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("NDP" "Party Identification", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesNDPPID_NonbinaryVSWoman.gph, replace	
restore

*** COMBINE LIBERAL, CONSERVATIVE, AND NDP GRAPHS
graph combine HQ_IssuesLiberalPID_NonbinaryVSMan.gph ///
			  HQ_IssuesLiberalPID_NonbinaryVSWoman.gph ///
			  HQ_IssuesConservativePID_NonbinaryVSMan.gph ///
			  HQ_IssuesConservativePID_NonbinaryVSWoman.gph ///
			  HQ_IssuesNDPPID_NonbinaryVSMan.gph ///
			  HQ_IssuesNDPPID_NonbinaryVSWoman.gph, ///
			  col(2)
graph export "HQ_IssuesPartyIdentification.png", height(1600) width(2400) replace

*** LIBERAL VOTE CHOICE
** MODEL 1: NO CONTROLS
mi estimate: logit vote_1 i.genderid [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model1IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (1) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 2: PRIOR DEMOGRAPHICS
mi estimate: logit vote_1 i.genderid i.age4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model2IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (2) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 3: ALL DEMOGRAPHICS
mi estimate: logit vote_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model3IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (3) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 4: ALL DEMOGRAPHICS + CONVERSION THERAPY
mi estimate: logit vote_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt i.conversion_therapy [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model4IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (4) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 5: ALL DEMOGRAPHICS + LEFT-RIGHT SCALE
mi estimate: logit vote_1 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model5IssuesLiberalPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (5) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

*** GRAPH MODELS FOR LIBERAL VOTE CHOICE
preserve
use "HQ_Model1IssuesLiberalPartyID", clear
append using "HQ_Model2IssuesLiberalPartyID"
append using "HQ_Model3IssuesLiberalPartyID"
append using "HQ_Model4IssuesLiberalPartyID"
append using "HQ_Model5IssuesLiberalPartyID"

*** CHECK RELATIVE SIZE OF GAPS
gen BiggerGap2 = 0
replace BiggerGap2 = 1 if abs(Gap2) > abs(Gap1)
tab BiggerGap2

gen BiggerGap3 = 0
replace BiggerGap3 = 1 if abs(Gap3) > abs(Gap1)
tab BiggerGap3

*** DROP THESE VARIABLES FOR RESHAPING
drop BiggerGap2 BiggerGap3

*** RESHAPE FROM MODEL-LEVEL TO MODEL-COMPARISON-LEVEL
reshape long Gap Lower Upper p, i(Model) j(Comparison)

*** LABEL COMPARISONS
label define comparisons 1 "Woman-Man" 2 "Nonbinary-Man" 3 "Nonbinary-Woman"
label values Comparison comparisons

*** CONVERT ESTIMATES FROM PROPORTIONS TO PERCENTAGES
replace Gap = Gap * 100
gen Lower95 = Lower * 100
gen Upper95 = Upper * 100
gen Lower90 = Gap - ((Gap - Lower95) * 0.837)
gen Upper90 = Gap + ((Upper95 - Gap) * 0.837)

*** CODE SIGNIFICANCE AT P < 0.1
gen Sig90 = 0
replace Sig90 = 1 if p < 0.1

gen Sig95 = 0
replace Sig95 = 1 if p < 0.05

*** LOOK AT SIGNIFICANCE BY COMPARISON
tab Comparison Sig90
tab Comparison Sig95
*** ALL GAPS IN LIBERAL VOTE CHOICE ARE SIGNIFICANT

*** REPLACE NUMBERS AND GENERATE VALUE LABELS
gen ModelLabelled = (Model - 6) * -1

label define models ///
			1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls"
label values ModelLabelled models
			
*** GRAPH COMPARISONS
graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 2, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 2, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 2, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Man") ///
	xlabel(-30(10)0) ///
	xscale(range(-35 5)) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Liberal" "Vote Choice", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesLiberalVote_NonbinaryVSMan.gph, replace	

graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 3, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 3, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 3, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Woman") ///
	xscale(range(-35 5)) ///
	xlabel(-30(10)0) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Liberal" "Vote Choice", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesLiberalVote_NonbinaryVSWoman.gph, replace	
restore

*** CONSERVATIVE VOTE CHOICE
** MODEL 1: NO CONTROLS
mi estimate: logit vote_2 i.genderid [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model1IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (1) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 2: PRIOR DEMOGRAPHICS
mi estimate: logit vote_2 i.genderid i.age4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model2IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (2) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 3: ALL DEMOGRAPHICS
mi estimate: logit vote_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model3IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (3) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 4: ALL DEMOGRAPHICS + CONVERSION THERAPY
mi estimate: logit vote_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt i.conversion_therapy [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model4IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (4) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 5: ALL DEMOGRAPHICS + LEFT-RIGHT SCALE
mi estimate: logit vote_2 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model5IssuesConservativePartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (5) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

*** GRAPH MODELS FOR CONSERVATIVE VOTE CHOICE
preserve
use "HQ_Model1IssuesConservativePartyID", clear
append using "HQ_Model2IssuesConservativePartyID"
append using "HQ_Model3IssuesConservativePartyID"
append using "HQ_Model4IssuesConservativePartyID"
append using "HQ_Model5IssuesConservativePartyID"

*** CHECK RELATIVE SIZE OF GAPS
gen BiggerGap2 = 0
replace BiggerGap2 = 1 if abs(Gap2) > abs(Gap1)
tab BiggerGap2

gen BiggerGap3 = 0
replace BiggerGap3 = 1 if abs(Gap3) > abs(Gap1)
tab BiggerGap3

*** DROP THESE VARIABLES FOR RESHAPING
drop BiggerGap2 BiggerGap3

*** RESHAPE FROM MODEL-LEVEL TO MODEL-COMPARISON-LEVEL
reshape long Gap Lower Upper p, i(Model) j(Comparison)

*** LABEL COMPARISONS
label define comparisons 1 "Woman-Man" 2 "Nonbinary-Man" 3 "Nonbinary-Woman"
label values Comparison comparisons

*** CONVERT ESTIMATES FROM PROPORTIONS TO PERCENTAGES
replace Gap = Gap * 100
gen Lower95 = Lower * 100
gen Upper95 = Upper * 100
gen Lower90 = Gap - ((Gap - Lower95) * 0.837)
gen Upper90 = Gap + ((Upper95 - Gap) * 0.837)

*** CODE SIGNIFICANCE AT P < 0.1
gen Sig90 = 0
replace Sig90 = 1 if p < 0.1

gen Sig95 = 0
replace Sig95 = 1 if p < 0.05

*** LOOK AT SIGNIFICANCE BY COMPARISON
tab Comparison Sig90
tab Comparison Sig95
*** ALL GAPS IN CONSERVATIVE VOTE CHOICE ARE SIGNIFICANT

*** REPLACE NUMBERS AND GENERATE VALUE LABELS
gen ModelLabelled = (Model - 6) * -1

label define models ///
			1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls"
label values ModelLabelled models
			
*** GRAPH COMPARISONS
graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 2, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 2, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 2, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Man") ///
	xlabel(-30(10)30) ///
	xscale(range(-35 35)) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Conservative" "Vote Choice", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesConservativeVote_NonbinaryVSMan.gph, replace	

graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 3, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 3, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 3, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Woman") ///
	xscale(range(-35 35)) ///
	xlabel(-30(10)30) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("Conservative" "Vote Choice", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesConservativeVote_NonbinaryVSWoman.gph, replace	
restore

*** NDP VOTE CHOICE
** MODEL 1: NO CONTROLS
mi estimate: logit vote_3 i.genderid [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model1IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (1) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 2: PRIOR DEMOGRAPHICS
mi estimate: logit vote_3 i.genderid i.age4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model2IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (2) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 3: ALL DEMOGRAPHICS
mi estimate: logit vote_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model3IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (3) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 4: ALL DEMOGRAPHICS + CONVERSION THERAPY
mi estimate: logit vote_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt i.conversion_therapy [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model4IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (4) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'

** MODEL 5: ALL DEMOGRAPHICS + LEFT-RIGHT SCALE
mi estimate: logit vote_3 i.genderid i.age4 i.education c.income7 i.sexualid4 i.born_outside_canada i.indigenous i.racialized i.mother_tongue4 i.pt c.left [pweight = cps21_weight_general_all]
mimrgns i.genderid , predict(pr) pwcompare

tempname memhold
postfile `memhold' Model Gap1 Gap2 Gap3 Lower1 Lower2 Lower3 Upper1 Upper2 Upper3 p1 p2 p3 ///
	using "HQ_Model5IssuesNDPPartyID", replace 

	matrix row = r(table_vs)
	local diff1 = row[1,1]
	local diff2 = row[1,2]
	local diff3 = row[1,3]
	local lower1 = row[5,1]
	local lower2 = row[5,2]
	local lower3 = row[5,3]
	local upper1 = row[6,1]
	local upper2 = row[6,2]
	local upper3 = row[6,3]
	local p1 = row[4,1]
	local p2 = row[4,2]
	local p3 = row[4,3]
post `memhold' (5) (`diff1') (`diff2') (`diff3') ///
				(`lower1') (`lower2') (`lower3') ///
				(`upper1') (`upper2') (`upper3') ///
				(`p1') (`p2') (`p3')
postclose `memhold'
*/

*** GRAPH MODELS FOR NDP VOTE CHOICE
preserve
use "HQ_Model1IssuesNDPPartyID", clear
append using "HQ_Model2IssuesNDPPartyID"
append using "HQ_Model3IssuesNDPPartyID"
append using "HQ_Model4IssuesNDPPartyID"
append using "HQ_Model5IssuesNDPPartyID"

*** CHECK RELATIVE SIZE OF GAPS
gen BiggerGap2 = 0
replace BiggerGap2 = 1 if abs(Gap2) > abs(Gap1)
tab BiggerGap2

gen BiggerGap3 = 0
replace BiggerGap3 = 1 if abs(Gap3) > abs(Gap1)
tab BiggerGap3

*** DROP THESE VARIABLES FOR RESHAPING
drop BiggerGap2 BiggerGap3

*** RESHAPE FROM MODEL-LEVEL TO MODEL-COMPARISON-LEVEL
reshape long Gap Lower Upper p, i(Model) j(Comparison)

*** LABEL COMPARISONS
label define comparisons 1 "Woman-Man" 2 "Nonbinary-Man" 3 "Nonbinary-Woman"
label values Comparison comparisons

*** CONVERT ESTIMATES FROM PROPORTIONS TO PERCENTAGES
replace Gap = Gap * 100
gen Lower95 = Lower * 100
gen Upper95 = Upper * 100
gen Lower90 = Gap - ((Gap - Lower95) * 0.837)
gen Upper90 = Gap + ((Upper95 - Gap) * 0.837)

*** CODE SIGNIFICANCE AT P < 0.1
gen Sig90 = 0
replace Sig90 = 1 if p < 0.1

gen Sig95 = 0
replace Sig95 = 1 if p < 0.05

*** LOOK AT SIGNIFICANCE BY COMPARISON
tab Comparison Sig90
tab Comparison Sig95
*** ALL GAPS IN NDP VOTE CHOICE ARE SIGNIFICANT

*** REPLACE NUMBERS AND GENERATE VALUE LABELS
gen ModelLabelled = (Model - 6) * -1

label define models ///
			1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls"
label values ModelLabelled models
			
*** GRAPH COMPARISONS
graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 2, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 2, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 2, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Man") ///
	xscale(range(-10 70)) ///
	xlabel(-10(10)70) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("NDP" "Vote Choice", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesNDPVote_NonbinaryVSMan.gph, replace	

graph twoway ///
	(scatter ModelLabelled Gap if Comparison == 3, mcolor(black) msymbol(Oh) mlabel(Gap) mlabposition(12) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike Lower90 Upper90 ModelLabelled if Comparison == 3, lcolor(black) lwidth(medthick) horizontal) ///
	(rspike Lower95 Upper95 ModelLabelled if Comparison == 3, lcolor(black) lwidth(thin) horizontal), ///
	ytitle("") ///
	ylabel(1 "All Demographics + Left-Right Scale" ///
			2 "All Demographics + Conversion Therapy" ///
			3 "All Demographics" ///
			4 "Prior Demographics" ///
			5 "No Controls") ///
	yscale(range(0.8 5.2)) ///
	xtitle("Nonbinary-Woman") ///
	xscale(range(-10 70)) ///
	xlabel(-10(10)70) ///
	xline(0, lcolor(gs14) lpattern(solid)) ///
	title("NDP" "Vote Choice", size(medsmall)) ///
	legend(off)
graph save HQ_IssuesNDPVote_NonbinaryVSWoman.gph, replace	
restore

*** COMBINE LIBERAL, CONSERVATIVE, AND NDP GRAPHS
graph combine HQ_IssuesLiberalVote_NonbinaryVSMan.gph ///
			  HQ_IssuesLiberalVote_NonbinaryVSWoman.gph ///
			  HQ_IssuesConservativeVote_NonbinaryVSMan.gph ///
			  HQ_IssuesConservativeVote_NonbinaryVSWoman.gph ///
			  HQ_IssuesNDPVote_NonbinaryVSMan.gph ///
			  HQ_IssuesNDPVote_NonbinaryVSWoman.gph, ///
			  col(2)
graph export "HQ_IssuesVoteChoice.png", height(1600) width(2400) replace